home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / sampleutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-02  |  7.4 KB  |  228 lines

  1. /*
  2.     file SampleUtils.c
  3.     
  4.     Description:
  5.     This file contains a number of utility routines used by the
  6.     HTMLSampleApplication.  These routines have been moved
  7.     here to a separate file to simplify the example.
  8.     
  9.     HTMLSample is an application illustrating how to use the new
  10.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  11.     is Apple's light-weight HTML rendering engine capable of
  12.     displaying HTML files.
  13.  
  14.     Copyright: © 1999 by Apple Computer, Inc.
  15.     All rights reserved.
  16.     
  17.     Disclaimer:
  18.     You may incorporate this sample code into your applications without
  19.     restriction, though the sample code has been provided "AS IS" and the
  20.     responsibility for its operation is 100% yours.  However, what you are
  21.     not permitted to do is to redistribute the source as "DSC Sample Code"
  22.     after having made changes. If you're going to re-distribute the source,
  23.     we require that you make it clear in the source that the code was
  24.     descended from Apple Sample Code, but that you've made changes.
  25.     
  26.     Change History (most recent first):
  27.     10/16/99 created
  28. */
  29.  
  30. #include "SampleUtils.h"
  31. #include <Processes.h>
  32. #include <string.h>
  33. #include <QuickDraw.h>
  34. #include <Gestalt.h>
  35. #include <Palettes.h>
  36. #include <Resources.h>
  37. #include <TextUtils.h>
  38.  
  39. #include <HTMLRendering.h>
  40.  
  41.  
  42.  
  43. /* SetWindowStandardStateSize sets the window's standard state rectangle
  44.     to a rectangle of the size suggested in the width and height parameters.
  45.     In the end, it may set the standard size to something smaller than the
  46.     width and height parameters so the entire window remains visible.  The
  47.     standard rectangle is also centered on the main screen.  The window's
  48.     standard state rectangle is used by ZoomWindow whenever when it
  49.     is called with the inZoomOut partcode. */
  50. void SetWindowStandardStateSize(WindowPtr window, short width, short height) {
  51.     Rect standard, stdRect, global, device, c, s, diffs;
  52.     RgnHandle sRgn, cRgn;
  53.     GDHandle theDevice;
  54.         /* make the window's port the current port */
  55.     SetPort(window);
  56.         /* find the window's monitor coordinates */
  57.     global = window->portRect;
  58.     LocalToGlobal((Point*) &global.top);
  59.     LocalToGlobal((Point*) &global.bottom);
  60.         /* get the maximum intersecting screen */
  61.     theDevice = GetMaxDevice(&global);
  62.     device = (**theDevice).gdRect;
  63.         /* if it's the main screen, adjust it for the menu bar height */
  64.     if (theDevice == GetMainDevice())
  65.         device.top += GetMBarHeight();
  66.         /* calculate the difference between the window's content rectangle and its frame */
  67.     sRgn = NewRgn();
  68.     cRgn = NewRgn();
  69.     GetWindowStructureRgn(window, sRgn);
  70.     GetWindowContentRgn(window, cRgn);
  71.     c = (**cRgn).rgnBBox;
  72.     s = (**sRgn).rgnBBox;
  73.     SetRect(&diffs, c.left - s.left + 2, c.top - s.top + 2, s.right - c.right + 2, s.bottom - c.bottom + 2);
  74.     DisposeRgn(sRgn);
  75.     DisposeRgn(cRgn);
  76.         /* calculate the maximum bounds for the standard rectangle */
  77.     SetRect(&standard, device.left + diffs.left, device.top + diffs.top, device.right - diffs.right, device.bottom - diffs.bottom);
  78.         /* set up our 'desired' rectangle */
  79.     SetRect(&stdRect, 0, 0, width, height);
  80.     OffsetRect(&stdRect, standard.left, standard.top);
  81.         /* fit it inside of the maximum allowable rectangle */
  82.     SectRect(&stdRect, &standard, &stdRect);
  83.         /* center it in the maximum allowable rectangle */
  84.     OffsetRect(&stdRect, (standard.right - stdRect.right)/2, (standard.bottom - stdRect.bottom)/2);
  85.         /* set the standard state rectangle */
  86.     SetWindowStandardState(window, &stdRect);
  87. }
  88.  
  89.  
  90.  
  91. /* GetApplicationFolder returns a URL referring to the folder
  92.     containing the application.  If successful, *url is set
  93.     to a new handle containing the URL.  It is the caller's
  94.     responsibility to dispose of this handle. */
  95. OSStatus GetApplicationFolderURL(Handle *url) {
  96.     OSStatus err;
  97.     FSSpec spec;
  98.     Handle theURL;
  99.         /* set up locals to a known state */
  100.     theURL = NULL;
  101.         /* find the application's folder */ 
  102.     err = GetApplicationFolder(&spec);
  103.     if (err != noErr) goto bail;
  104.         /* create a new handle for storing the URL */
  105.     theURL = NewHandle(0);
  106.     if (theURL == NULL) { err = memFullErr; goto bail; }
  107.         /* ask the HTML rendering library to convert
  108.         the FSSpec into a URL */
  109.     err = HRUtilGetURLFromFSSpec(&spec, theURL);
  110.     if (err != noErr) goto bail;
  111.     
  112.         /* add a slash at the end of the name, if one is not there */
  113.     if ( (*theURL)[GetHandleSize(theURL) - 1] != '/')
  114.         Munger(theURL, (GetHandleSize(theURL) - 1), NULL, 0, "/", 1);
  115.  
  116.         /* return the URL */
  117.     *url = theURL;
  118.     return noErr;
  119. bail:
  120.     if (theURL != NULL) DisposeHandle(theURL);
  121.     return err;
  122. }
  123.  
  124.  
  125. /* GetApplicationFolder returns the volume reference number and
  126.     directory id of the folder containing the application. */
  127. OSStatus GetApplicationFolder(FSSpec *spec) {
  128.     OSStatus err;
  129.     FCBPBRec fcpb;
  130.     Str255 name;
  131.     CInfoPBRec cat;
  132.     Handle h;
  133.     
  134.     h = GetResource('vTeZ', 0);
  135.     if (h == NULL) return resNotFound;
  136.     
  137.     memset(&fcpb, 0, sizeof(fcpb));
  138.     fcpb.ioVRefNum = 0;
  139.     fcpb.ioNamePtr = name;
  140.     fcpb.ioFCBParID = 0;
  141.     fcpb.ioRefNum = HomeResFile(h);
  142.     fcpb.ioFCBIndx = 0;
  143.     if ((err = PBGetFCBInfoSync(&fcpb)) != noErr) return err;
  144.     
  145.     memset(&cat, 0, sizeof(cat));
  146.     cat.dirInfo.ioNamePtr = name;
  147.     cat.dirInfo.ioVRefNum = fcpb.ioFCBVRefNum;
  148.     cat.dirInfo.ioFDirIndex = -1;
  149.     cat.dirInfo.ioDrDirID = fcpb.ioFCBParID;
  150.     err = PBGetCatInfoSync(&cat);
  151.     if (err != noErr) return err;
  152.     
  153.     err = FSMakeFSSpec(fcpb.ioFCBVRefNum, cat.dirInfo.ioDrParID, name, spec);
  154.     if (err != noErr) return err;
  155.     
  156.     return noErr;
  157. }
  158.  
  159. /* DrawGrowIconWithoutScrollLines draws the grow icon in the bottom
  160.     right corner of the frontmost window without drawing the scroll bar
  161.     lines.  It does this by setting the clip region to the bottom right corner
  162.     before calling DrawGrowIcon. */
  163. void DrawGrowIconWithoutScrollLines(WindowPtr window) {
  164.     RgnHandle clip;
  165.     Rect r = { 0, 0, 16, 16 };
  166.         /* set the window to the current port */
  167.     SetPort(window);
  168.         /* save the old clipping region */
  169.     clip = NewRgn();
  170.     if (clip == NULL) return;
  171.     GetClip(clip);
  172.         /* set the clipping region to the bottom right corner of the window */
  173.     OffsetRect(&r, window->portRect.right-15, window->portRect.bottom-15);
  174.     ClipRect(&r);
  175.         /* draw the grow icon */
  176.     DrawGrowIcon(window);
  177.     SetPort(window);
  178.     SetClip(clip);
  179.     DisposeRgn(clip);
  180. }
  181.  
  182.  
  183.  
  184.  
  185. /* GrayOutBox grays out an area of the screen in the current grafport.  
  186.     *theBox is in local coordinates in the current grafport. This routine
  187.     is for direct screen drawing only.  */
  188. void GrayOutBox(Rect *theBox) {
  189.     long response;
  190.     Rect globalBox;
  191.     GDHandle maxDevice;
  192.     RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}, rgbBlack = {0, 0, 0}, sForground, sBackground;
  193.     PenState penSave;
  194.         /* save the current drawing state */
  195.     GetPenState(&penSave);
  196.         /* if no color quickdraw, fail...*/
  197.     if (Gestalt(gestaltQuickdrawVersion, &response) != noErr) response = 0;
  198.     if (response >= gestalt8BitQD) {
  199.             /* get the device for the rectangle */
  200.         globalBox = *theBox;
  201.         LocalToGlobal((Point*) &globalBox.top);
  202.         LocalToGlobal((Point*) &globalBox.bottom);
  203.         maxDevice = GetMaxDevice(&globalBox);
  204.         if (maxDevice != NULL) {
  205.                 /* calculate the best gray */
  206.             if ( GetGray(maxDevice, &rgbWhite, &rgbBlack)) {
  207.                     /* draw over the area in gray using addMax transfer mode */
  208.                 GetForeColor(&sForground);
  209.                 GetBackColor(&sBackground);
  210.                 RGBForeColor(&rgbBlack);
  211.                 RGBBackColor(&rgbWhite);
  212.                 PenMode(addMax);
  213.                 PaintRect(theBox);
  214.                 RGBForeColor(&sForground);
  215.                 RGBBackColor(&sBackground);
  216.                     /* restore the pen state and leave */
  217.                 SetPenState(&penSave);
  218.                 return;
  219.             }
  220.         }
  221.     }
  222.         /* fall through to using the gray pattern */
  223.     PenPat(&qd.gray);
  224.     PenMode(notPatBic);
  225.     PaintRect(theBox);
  226.     SetPenState(&penSave);
  227. }
  228.